/*
 * CS_BLACKSMITH_UPSTONE.cpp
 *
 *  Created on: 2013. 5. 27.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NLogic/Bag.h"
#include "../../NLogic/Item.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_BLACKSMITH_UPSTONE(User * pUser, ::Network::Packet * pPacket)
{
/*	ZNO_CS_BLACKSMITH_UPSTONE  //0x2204
	{
		// 무조건 2개 아이템
		u64	u64item_seq;		// 아이템 시컨스
		u8	u8bag_type;
		u64	u64item_seq2;		// 아이템 시컨스
		u8	u8bag_type2;
	}

	// 강화석 생성 의뢰 응답
	ZNO_SC_BLACKSMITH_UPSTONE  //0x2205
	{
		s16	s16_ack;		// 결과 (352 : 등급 불일치)
	}*/

	PREPARE_PACKET;

	uint64_t	itemSeq		= 0;
	uint8_t		bagType		= 0;
	uint64_t	itemSeq2	= 0;
	uint8_t		bagType2	= 0;

	*pPacket >> itemSeq >> bagType >> itemSeq2 >> bagType2;

	NLogic::Bag * pBag	= pUser->GetBag(static_cast<INVEN_BAG_TYPE>(bagType));
	NLogic::Bag * pBag2	= pUser->GetBag(static_cast<INVEN_BAG_TYPE>(bagType2));

	if (pBag == NULL || pBag2 == NULL)
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE : pBag is NULL! bagtype=%d, bagtype2=%d", bagType, bagType2);
		return;
	}

	NLogic::Item * pItem	= pBag->GetItem(itemSeq);
	NLogic::Item * pItem2	= pBag2->GetItem(itemSeq2);

	if (pItem == NULL || pItem2 == NULL)
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE : pItem is NULL! itemseq=%lld, itemseq2=%lld", itemSeq, itemSeq2);
		return;
	}

	NResource::BalanceData * pBalance = NResource::Static::InstancePtr()->GetBalance()->GetBalancePtr(pUser);
	if (NULL == pBalance)
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE : pBalance is NULL!");
		return;
	}

	// 같은 등급의 아이템만 강화석 제작 가능
	if (pItem->GetData().quality != pItem2->GetData().quality)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		*pAck << static_cast<int16_t>(ACK_UPSTONE_GRADE_MISMATCH);
		pAck->MakePacket(CMD_SC_BLACKSMITH_UPSTONE);
		pUser->Send(pAck);
		return;
	}

	// 아이템 레벨 계산 -> (item1 + item2) / 2
	int combineLevel	= (pItem->GetData().level + pItem2->GetData().level) / 2;

	// 강화석 제작 소요 시간 계산 -> item_level * 조합시간상수
	int combineElapsed	= combineLevel * pBalance->GetValue(EM_UPSTONE_COMBINE_TIME_CONSTANT);

	// 강화석 제작 가격 계산	-> ((제작시간 / 25) * 조합가격상수) + 조합가격상수
	int needZen			= ((combineElapsed / 25) * pBalance->GetValue(EM_UPSTONE_COMBINE_PRICE_CONSTANT)) + pBalance->GetValue(EM_UPSTONE_COMBINE_PRICE_CONSTANT);

	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::S_PROCEDURE);
	clsAuto->MakeQuery("call sp_insert_data_upstone(%d,%d,%d,%d,%d)",
													pUser->GetCharacterInfo().characterseq,
													combineLevel,
													pItem->GetData().quality,
													combineElapsed,
													needZen);

	if (false == ::Database::GETSESSION()->SyncExecute(pUser->GetUserInfo().shard_Item_db_type, clsAuto))
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE : false == ::Database::GETSESSION()->SyncExecute(1, clsAuto)");
		return;
	}

	if (clsAuto->GetErrorIdx() != 0)
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE : clsAuto->GetErrorIdx() != 0");
		return;
	}

	pBag->PopItem(itemSeq);
	pItem->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_UPSTONE);

	pBag2->PopItem(itemSeq2);
	pItem2->DeleteDatabaseItemInfo(NLogic::Item::ItemStatus::EM_ITEM_STATUS_UPSTONE);

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	*pAck << static_cast<int16_t>(ACK_OK);
	pAck->MakePacket(CMD_SC_BLACKSMITH_UPSTONE);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
